package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.mapper.CourseCategoryMapper;
import com.b2.mapper.CourseMapper;
import com.b2.mapper.VideoMapper;
import com.b2.pojo.dto.CourseDto;
import com.b2.pojo.dto.CourseRecommendDto;
import com.b2.pojo.entity.Course;
import com.b2.pojo.entity.CourseCategory;
import com.b2.pojo.entity.Video;
import com.b2.result.Result;
import com.b2.service.CourseCategoryService;
import com.b2.service.CourseService;
import com.b2.utils.AliOssUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 悦神
* @description 针对表【course】的数据库操作Service实现
* @createDate 2024-04-30 11:52:41
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private VideoMapper videoMapper;

    @Resource
    private AliOssUtil aliOssUtil;

    @Resource
    private CourseCategoryService courseCategoryService;

    @Resource
    private CourseCategoryMapper courseCategoryMapper;


    @Override
    @Transactional
    public Result<List<Course>> add(Course course) {
        // 判断标记类型名是否为空
        extracted(course, false);
        // 添加课程
        courseMapper.insert(course);
        // 回显数据
        List<Course> lists = courseMapper.selectList(new QueryWrapper<>());
        // 返回响应结果
        return Result.success(lists);
    }

    @Override
    @Transactional
    public Result<List<Course>> delete(Long courseId) {
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<Course> originalQueryWrapper = new LambdaQueryWrapper<>();
        // 设置categoryId条件
        originalQueryWrapper.eq(Course::getCourseId, courseId);
        // 通过categoryId查找数据
        Course originalCourse = courseMapper.selectOne(originalQueryWrapper);
        // 判断数据是否存在
        if (originalCourse == null) {
            throw new RuntimeException(MessageConstant.COURSE_NOT_EXIST);
        }
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        // 设置courseId条件
        queryWrapper.eq(Video::getCourseId, courseId);
        // 通过courseId查找视频资源
        List<Video> videoLists = videoMapper.selectList(queryWrapper);
        // 判断数据是否存在
        if (!videoLists.isEmpty()) {
            throw new RuntimeException(MessageConstant.NOT_ALLOW_DELETE);
        }
        // 通过courseId获取课程封面url
        String CourseCoverUrl = courseMapper.selectById(courseId).getCourseCoverUrl();
        // 通过courseId删除课程封面
        videoMapper.deleteById(courseId);
        // 将阿里云oss的封面删除
        aliOssUtil.deleteFile(CourseCoverUrl);
        courseMapper.delete(originalQueryWrapper);
        // 回显数据
        List<Course> lists = courseMapper.selectList(new QueryWrapper<>());
        // 返回响应结果
        return Result.success(lists);
    }

    @Override
    public Result<String> update(Course course) {
        extracted(course, true);
        courseMapper.updateById(course);
        return Result.success();
    }



    @Override
    public Result<List<CourseDto>> selectList(String courseAuthority) {
        List<CourseCategory> courseCategoryList = courseCategoryService.list();
        List<CourseDto> courseDtoList = new ArrayList<>();
        for (CourseCategory courseCategory : courseCategoryList) {
            CourseDto courseDto = new CourseDto();
            List<Course> courseList = this.getList(courseCategory.getCategoryId(), courseAuthority);
            if(courseList.size() != 0) {
                courseDto.setCategoryName(courseCategory.getCategoryName());
                courseDto.setCourseList(courseList);
                courseDtoList.add(courseDto);
            }
        }
        return Result.success(courseDtoList);
    }

    @Override
    public Result<List<Course>> select(Long categoryId, String courseAuthority) {
        List<Course> courseList = this.getList(categoryId, courseAuthority);
        return Result.success(courseList);
    }

    @Override
    @Transactional
    public Result<String> setRecommend(Long courseId, Integer courseRecommend) {
        // 设置课程推荐类型
        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseRecommend(courseRecommend);
        courseMapper.updateById(course);
        return Result.success();
    }

    @Override
    public Result<List<CourseRecommendDto>> recommendList(Integer courseRecommend, String courseAuthority) {
        // 查询课程推荐
        List<Course> courseList = getCourses(courseRecommend, courseAuthority);
        List<CourseRecommendDto> courseRecommendDtoList = new ArrayList<>();
        for (Course course : courseList) {
            CourseCategory courseCategory = courseCategoryMapper.selectById(course.getCategoryId());
            CourseRecommendDto courseRecommendDto = new CourseRecommendDto();
            courseRecommendDto.setCourseCategoryName(courseCategory.getCategoryName());
            courseRecommendDto.setCourse(course);
            courseRecommendDtoList.add(courseRecommendDto);
        }
        return Result.success(courseRecommendDtoList);
    }

    private List<Course> getList(Long categoryId, String courseAuthority) {
        // 根据类型id和课程权限查询课程
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(categoryId != null, Course::getCategoryId, categoryId);
        lambdaQueryWrapper.eq(courseAuthority != null, Course::getCourseAuthority, courseAuthority);
        return courseMapper.selectList(lambdaQueryWrapper);
    }

    private List<Course> getCourses(Integer courseRecommend, String courseAuthority) {
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(courseRecommend == null) {
            lambdaQueryWrapper.ne(Course::getCourseRecommend, 0);
        } else {
            lambdaQueryWrapper.inSql(Course::getCourseRecommend, courseRecommend + ",3");
        }
        lambdaQueryWrapper.eq(Course::getCourseAuthority, courseAuthority);
        return courseMapper.selectList(lambdaQueryWrapper);
    }

    // 判断数据是否存在
    private void extracted(Course course,boolean flag) {
        // 判断标记类型名是否为空
        if(course.getCourseName() == null || course.getCourseName().equals("")) {
            throw new RuntimeException(MessageConstant.COURSE_EMPTY);
        }
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置条件
        lambdaQueryWrapper.eq(Course::getCourseName, course.getCourseName());
        lambdaQueryWrapper.eq(Course::getCategoryId, course.getCategoryId());
        lambdaQueryWrapper.eq(Course::getCourseAuthority, course.getCourseAuthority());
        // 如果是修改的话，查询时不包括自身
        if(flag) {
            lambdaQueryWrapper.ne(Course::getCourseId, course.getCourseId());
        }
        Course originalCourse = courseMapper.selectOne(lambdaQueryWrapper);
        // 判断数据是否存在
        if (originalCourse != null) {
            throw new RuntimeException(MessageConstant.COURSE_EXIST);
        }

        course.setUpdateTime(LocalDateTime.now());
    }
}




